package com.yu.waterstation.app.moudles.ticketorder.controller;


import com.yu.waterstation.app.common.util.session.SessionUtil;
import com.yu.waterstation.app.moudles.ticketorder.service.TicketOrderService;
import com.yu.waterstation.common.common.annotation.NeedToken;
import com.yu.waterstation.common.common.annotation.VisitorAccess;
import com.yu.waterstation.common.common.enums.TokenType;
import com.yu.waterstation.common.common.exception.ServiceException;
import com.yu.waterstation.common.common.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "水票订单信息")
@RestController
@RequestMapping("/ticketOrder")
public class TicketOrderController {

    @Resource
    private TicketOrderService ticketOrderService;


    @ApiOperation("寝室的水票订单历史记录")
    @VisitorAccess
    @GetMapping("/address/{addressId}")
    public Result getOrderHistoryByAddressId(@PathVariable Integer addressId) {
        return Result.ok(ticketOrderService.getOrderHistoryByAddressId(addressId));
    }


    @ApiOperation("创建寝室水票订单")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_student_token)
    @PostMapping("/{ticketGoodsId}")
    public Result createOrder(@PathVariable Integer ticketGoodsId, String payType,  HttpServletRequest request) throws ServiceException {
        return Result.ok(ticketOrderService.createOrder(ticketGoodsId, payType, SessionUtil.getStudentIdFromRequest(request)));
    }

    @ApiOperation("取消寝室的水票订单")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_student_token)
    @DeleteMapping("/{orderId}")
    public Result cancelOrder(@PathVariable Integer orderId, HttpServletRequest request) {
        return Result.ok(ticketOrderService.cancelOrder(orderId, SessionUtil.getStudentIdFromRequest(request)));
    }
}
